package Jogo.Entities{
	import Jogo.States.PhaseState;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/** Describer a turret point in the game (a place where
	 * the player can build turrets.
	 * 
	 * @author Bruno Zumba (05/08/2011)
	 */
	public class TurretPoint extends Entity{
		public static const TP_CLICKED : String = "Turret_Point_Clicked";
		/* Pictures */
		[Embed(source = "../../Img/PhaseState/turretPoint.png")]
		private var turretPointBMP : Class; 
		private var turretPointImg : Bitmap;
		
		/* Indicates wich player this turret point belongs to */
		private var player : int;
		private var playerTF : TextField;
		private var format : TextFormat;
		
		public static const TURRET_POINT_WIDTH : int = 50;
		public static const TURRET_POINT_HEIGHT : int = 50;
		
		private var turretPoint : MovieClip; //Holds the image of the turret point
		
		/* The x and y coordinates of this turret tile in the
		* tile matrix of the phase
		*/
		private var point : Point;
		
		/* The tile where the turret's in */
		private var tile : Tile;
		
		public function TurretPoint(point_p : Point) {
			point = point_p;
			tile = PhaseState.getTilesMatrix()[point.y][point.x];
			
			tile.setType(Tile.TURRET_POINT);
			tile.setTurretPoint(this);
			
			turretPointImg = new turretPointBMP();
			turretPointImg.width = TURRET_POINT_WIDTH;
			turretPointImg.height = TURRET_POINT_HEIGHT;
			root = new MovieClip();
			turretPoint = new MovieClip();
			playerTF = new TextField();
			
			turretPointImg.x -= turretPointImg.width/2;
			turretPointImg.y -= turretPointImg.height/2;
			turretPoint.addChild(turretPointImg);
			
			turretPoint.x = tile.getCenter().x;
			turretPoint.y = tile.getCenter().y;
			root.addChild(turretPoint);
			
			playerTF.x -= turretPointImg.width/2+10;
			playerTF.y -= turretPointImg.height/2+10;
			format = new TextFormat();
			format.font = "Discognate Regular";
			format.color = 0x000000;
			playerTF.defaultTextFormat = format;
			turretPoint.addChild(playerTF);
			
			
		}
		
		public function getTurretPointMC() : MovieClip{
			return turretPoint;
		}
		
		public function getPoint() : Point{
			return point;
		}
		
		/* Getters and Setters */
		public function getPlayer() : int {
			return player;
		}
		public function setPlayer(player_p : int) : void{
			player = player_p;
			playerTF.text = player.toString();
		}
		
		public function destroy() : void{
			
			turretPoint.removeChild(turretPointImg);
			root.removeChild(turretPoint);
			
			tile.setType(Tile.TURRET);
			tile.setTurretPoint(null);
			
			root = null;
			turretPoint = null;
			turretPointImg = null;
			playerTF = null;
			
		}
		
		
	}
}